package com.efast.cafe.portal.controller.statemach;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.efast.cafe.commponent.statemach.bean.StateMachTaskRetBean;
import com.efast.cafe.commponent.statemach.bean.TaskParamBean;
import com.efast.cafe.commponent.statemach.service.IPortalStateMachTaskHistoryService;
import com.efast.cafe.commponent.statemach.service.IPortalStateMachTaskService;
import com.efast.cafe.framework.bean.ResultBean;
import com.efast.cafe.framework.controller.AbstractController;
import com.efast.cafe.framework.util.SpringWebUtils;

@Controller
@RequestMapping("/statemach")
public class PortalStateMachTaskController extends AbstractController {

	@Autowired
	private IPortalStateMachTaskService taskService;

	@Autowired
	private IPortalStateMachTaskHistoryService stateMachTaskHistoryService;

	

	/**
	 * 根据taskId获取task
	 * 
	 * @param request
	 * @param response
	 * @param taskParam
	 * @return
	 */
	@RequestMapping(value = "/taskinfo")
	public @ResponseBody ResultBean getTaskbyId(HttpServletRequest request, HttpServletResponse response,
			@RequestBody TaskParamBean taskParam) {
		ResultBean result = new ResultBean(true, null);
		StateMachTaskRetBean data = null;
		if (taskParam.getTaskId() == null || taskParam.getTaskId() == 0) {
			data = taskService.getTaskBeanByConfId(SpringWebUtils.getRequestCompany(), taskParam.getConfId());
		} else {
			data = taskService.getTaskBeanById(SpringWebUtils.getRequestCompany(), taskParam.getTaskId(), true);
		}
		result.setObject(data);
		return result;
	}

	@RequestMapping(value = "/taskbyrel")
	public @ResponseBody ResultBean getTaskbyRelId(HttpServletRequest request, HttpServletResponse response,
			@RequestBody TaskParamBean taskParam) {
		ResultBean result = new ResultBean(true, null);
		result.setObject(taskService.getTaskbyRelId(taskParam.getRelId(), taskParam.getRelType()));
		return result;
	}

	/**
	 * 根据taskId获取流程历史
	 * @param request
	 * @param response
	 * @param taskParam
	 * @return
	 */
	@RequestMapping(value = "/taskhistory")
	public @ResponseBody ResultBean getTaskHistoryByTaskId(HttpServletRequest request, HttpServletResponse response,
			@RequestBody TaskParamBean taskParam) {
		ResultBean result = new ResultBean(true, null);
		result.setObject(stateMachTaskHistoryService.queryTaskHistoryBeanByTaskId(taskParam.getTaskId()));
		return result;
	}

}
